//
//  UIImage+TZCategory.swift
//  SimpleTZ
//
//  Created by wangfeng on 2020/7/13.
//  Copyright © 2020 davis. All rights reserved.
//

import UIKit

extension UIImage {
    class func imageWithColor(color: UIColor) -> UIImage? {
        let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
        UIGraphicsBeginImageContext(rect.size)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        context.setFillColor(color.cgColor)
        context.fill(rect)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image
    }
    
    /// 设置图片内容颜色
    func setTintColor(color: UIColor) -> UIImage? {
        
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        guard let context = UIGraphicsGetCurrentContext() else {
            return nil
        }
        context.translateBy(x: 0, y: size.height)
        context.scaleBy(x: 1.0, y: -1.0)
        context.setBlendMode(CGBlendMode.normal)
        
        let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
        if let cgImg = cgImage {
            context.draw(cgImg, in: rect)
        }
        context.setBlendMode(CGBlendMode.sourceIn)
        color.setFill()
        context.fill(rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage
    }
    
    /// 旋转图片
    func rotationImage(_ orientation: UIImage.Orientation) -> UIImage {
        let filpImage = UIImage(cgImage: self.cgImage!, scale: self.scale, orientation: orientation)
        return filpImage
    }
    
    /// 压缩图片
    func compressSize(maxImageLength: CGFloat = 0, maxSizeKB: CGFloat = 1024) -> Data? {
        return compressSize(maxSize: CGSize(width: maxImageLength, height: maxImageLength), maxSizeKB: maxSizeKB)
    }
    /// 压缩图片
    func compressSize(maxSize: CGSize = CGSize.zero, maxSizeKB: CGFloat = 1024) -> Data? {
        var maxKB = maxSizeKB
        var maxImageSize = maxSize
        if (maxKB <= 0) {
            maxKB = 1024
        }
        if (maxImageSize.width <= 0)  {
            maxImageSize.width = 1024
        }
        if (maxImageSize.height <= 0)  {
            maxImageSize.height = 1024
        }
        
        //先调整分辨率
        var newSize = CGSize(width: self.size.width, height: self.size.height)
        
        let tempHeight = newSize.height / maxImageSize.height;
        let tempWidth = newSize.width / maxImageSize.width;
        if (tempWidth > 1.0 && tempWidth > tempHeight) {
            newSize = CGSize(width: self.size.width / tempWidth, height: self.size.height / tempWidth)
        } else if (tempHeight > 1.0 && tempWidth < tempHeight) {
            newSize = CGSize.init(width: self.size.width / tempHeight, height: self.size.height / tempHeight)
        }
        UIGraphicsBeginImageContext(newSize)
        self.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
        if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
            UIGraphicsEndImageContext()
            if var imageData = newImage.jpegData(compressionQuality: 1.0) {
                var sizeOriginKB : CGFloat = CGFloat(imageData.count) / 1024.0
                //调整大小
                var resizeRate: CGFloat = 0.9;
                while (sizeOriginKB > maxKB && resizeRate > 0.1) {
                    if let newData = newImage.jpegData(compressionQuality: resizeRate) {
                        imageData = newData
                        sizeOriginKB = CGFloat(imageData.count) / 1024.0;
                        resizeRate -= 0.1;
                    }
                }
                return imageData
            }
        }
        return nil
    }
}

